package June;

public class Test05RMBConverter01 {

    private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] CN_UPPER_MONETARY_UNIT = {"分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"};

    public static String convert(double money) {
        long moneyInCent = Math.round(money * 100); // 转换为分

        if (moneyInCent > Long.MAX_VALUE || moneyInCent < Long.MIN_VALUE) {
            throw new IllegalArgumentException("超出转换范围");
        }

        StringBuilder sb = new StringBuilder();
        int index = 0;
        boolean needZero = false; // 是否需要补零

        while (moneyInCent > 0) {
            int num = (int) (moneyInCent % 10);
            if (num == 0) {
                if (index == 2 || index == 6 || index == 10) { // 元、万、亿位不补零
                    needZero = true;
                } else {
                    if (needZero) {
                        sb.insert(0, CN_UPPER_NUMBER[0]);
                    }
                    needZero = false;
                }
                sb.insert(0, CN_UPPER_MONETARY_UNIT[index]);
            } else {
                sb.insert(0, CN_UPPER_NUMBER[num] + CN_UPPER_MONETARY_UNIT[index]);
                needZero = false;
            }
            moneyInCent /= 10;
            index++;
        }

        if (sb.charAt(0) == '零') {
            sb.deleteCharAt(0); // 删除开头的零
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        double money = 11111111.12; // 需要转换的金额
        String rmb = convert(money);
        System.out.println("人民币大写：" + rmb);
    }
}